package com.samapp.map;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dragon.httpclient_util.HttpClientThread;
import com.dragon.httpclient_util.HttpResultHandle;
import com.dragon.httpclient_util.JsonUtils;
import com.samapp.MyApplication;
import com.samapp.R;

public class AddClientActivity extends Activity implements OnClickListener {

	private Button btnBack = null;
	private Button btnSubmit = null;
	private EditText etNumber  = null;
	private EditText etCname = null;
	private EditText etEname = null;
	private EditText etType = null;
	private EditText etCcontacts = null;
	private EditText etEcontacts = null;
	private EditText etFax = null;
	private EditText etEmail = null;
	private EditText etCaddress = null;
	private EditText etEaddress = null;
	private EditText etRemark = null;
	
	private MyApplication myApplication = MyApplication.getApplicationInstance();
	
	private final String[] text = { " " };

	/** 创建订单 */
	public static final int MSG_CREATE_CLIENT = 0X194;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == MSG_CREATE_CLIENT) {
				Toast.makeText(AddClientActivity.this, text[0], Toast.LENGTH_SHORT).show();
			}
		}
	};

	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_client);
		init();
	}

	private void init() {
		initView();
		initListener();
	}

	private void initView() {
		btnBack = (Button) findViewById(R.id.btn_back);
		btnSubmit = (Button) findViewById(R.id.btn_submit);
		etNumber = (EditText) findViewById(R.id.number);
		etCname = (EditText) findViewById(R.id.cname);
		etEname = (EditText) findViewById(R.id.ename);
		etType = (EditText) findViewById(R.id.type);
		etCcontacts = (EditText) findViewById(R.id.ccontacts);
		etEcontacts = (EditText) findViewById(R.id.econtacts);
		etFax = (EditText) findViewById(R.id.fax);
		etEmail = (EditText) findViewById(R.id.email);
		etCaddress = (EditText) findViewById(R.id.caddress);
		etEaddress = (EditText) findViewById(R.id.eaddress);
		etRemark = (EditText) findViewById(R.id.remark);
	}
	
	private void initListener() {
		btnBack.setOnClickListener(this);
		btnSubmit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			skip(ClientListActivity.class);
			finish();
			break;
		case R.id.btn_submit:
			saveData();
			finish();
			break;
		}
	}
	
	private void saveData(){
		String url = myApplication.url + "createClient.htm";
		Map<String, String> myMap = new HashMap<String, String>();
		myMap.put("accessToken", "5C9.yx0S");
		myMap.put("number", etNumber.getText().toString().trim());
		myMap.put("cname", etCname.getText().toString().trim());
		myMap.put("ename", etEname.getText().toString().trim());
		myMap.put("type", etType.getText().toString().trim());
		myMap.put("ccontacts", etCcontacts.getText().toString().trim());
		myMap.put("econtacts", etEcontacts.getText().toString().trim());
		myMap.put("fax", etFax.getText().toString().trim());
		myMap.put("email", etEmail.getText().toString().trim());
		myMap.put("caddress", etCaddress.getText().toString().trim());
		myMap.put("eaddress", etEaddress.getText().toString().trim());
		myMap.put("remark", etRemark.getText().toString().trim());
		HttpClientThread.getmHttpClientThread().post(url, myMap, new HttpResultHandle() {

			@Override
			public void onSuccess(String response) {
				String code = JsonUtils.getValue(response, "code");
				if ("0".equals(code)) {
					// text[0] = JsonUtils.getValue(response, "discount");
					text[0] = JsonUtils.getValue(response, "msg");
					mHandler.sendEmptyMessage(MSG_CREATE_CLIENT);
					finish();
				} else {
					text[0] = JsonUtils.getValue(response, "msg");
					mHandler.sendEmptyMessage(MSG_CREATE_CLIENT);
				}
			}

			@Override
			public void onFailure(int statusCode) {
				text[0] = "连接服务器失败，状态码：" + statusCode;
				mHandler.sendEmptyMessage(MSG_CREATE_CLIENT);
			}
		});
	}

	/** 跳转页面 */
	private void skip(Class<?> cls) {
		Intent intent = new Intent(this, cls);
		startActivity(intent);
	}
	
	/** 修改返回键 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		btnBack.performClick();
	}

}
